var areaUtils = (function() {
	var BASE_URL = 'http://182.61.20.153:8181/api';
	var _networkClient;
	var setNetworkClient = function(networkClient) {
		_networkClient = networkClient;
	}
	var getCityListLocalData = function() {
		var cityList = common.myStorage.getItem('cityList');
		if(cityList != null && cityList.length > 0) {
			return cityList;
		}
		
	}
	var getAreaListByLevel = function(level, callback) {

		//grade(0全国, 1省、经济特区, 2市, 3县级市, 4区)
		var paramaArea = [{
			key: 'grade',
			value: level
		}];
		
		if(level == 2) {
			
			var cityList = getCityListLocalData();
			if(cityList != null && cityList.length > 0) {
				callback && callback(cityList);
				return;
			}

		}

		
		//获取地区列表
		_networkClient.get('/area/areaList.json', paramaArea, function(data) {
			
			//成功
			if(level == 2) {
				common.myStorage.setItem('cityList', data.areaList);
			}
			callback && callback(data.areaList);

		}, function(message) {
			//失败
		});
	}
	var getCurrentCity = function() {
		var currentCity = common.myStorage.getItem('currentCity');
		if(currentCity != null) {
			return currentCity;
		} else {
			return null;
		}
	}
	var setCurrentCityByCityName = function(cityName) {
		var cityList = getCityListLocalData();
		if(cityList != null && cityList.length > 0) {
			var currentCity = new Object;
			for(var i = 0; i < cityList.length; i++) {
				var tempCity = cityList[i];
				if(tempCity.areaName.indexOf(cityName) >= 0) {
					currentCity = tempCity;
					break;
				}
			}
			setCurrentCityByCityObj(currentCity);
		}
	}
	var setCurrentCityByCityObj = function(cityObj) {
		var currentCity = getCurrentCity();
		if(currentCity == null || cityObj.identId != currentCity.identId) {
			common.myStorage.setItem('currentCity', cityObj);
			plus.storage.setItem('currentCity', cityObj);
			getChildAreaListWithParentArea(cityObj, true);
		}
	}
	var getChildAreaListWithParentArea = function(areaObj, needFinalLevelArea, callback) {

		var paramaArea = [{
			key: 'identId',
			value: areaObj.identId
		}];
		
			console.log(needFinalLevelArea);
		//获取子地区列表
		_networkClient.get('/area/childrenArea.json', paramaArea, function(data) {
			//成功
			if(needFinalLevelArea) {
				getFinalLevelArea(0, data.areaList, callback);
			} else {
				callback && callback(data.areaList);

			}

		}, function(message) {
			//失败
			console.log(message);
		});
	}
	
	/*根据城市identId获取*/
	var getChildAreaListWithParentAreaId = function(areaObjId, needFinalLevelArea, callback) {

		var paramaArea = [{
			key: 'identId',
			value: areaObjId
		}];
		
		//获取子地区列表
		_networkClient.get('/area/childrenArea.json', paramaArea, function(data) {
			//成功
			if(needFinalLevelArea) {
				getFinalLevelArea(0, data.areaList, callback);
			} else {
				callback && callback(data.areaList);

			}

		}, function(message) {
			//失败
			console.log(message);
		});
	}
	/*获取最末级地区数据*/
	var getFinalLevelArea = function(index, areaList, callback) {
		if(index == areaList.length) {
			common.myStorage.setItem('CurrentChildAreaListKey', areaList);
			callback && callback(areaList);
		} else {
			var areaObj = areaList[index];
			var paramaArea = [{
				key: 'identId',
				value: areaObj.identId
			}];
			//获取子地区列表
			_networkClient.get('/area/childrenArea.json', paramaArea, function(data) {
				//成功
				areaList[index].arrayChildArea = data.areaList;
				var finalAreaList = areaList[index].arrayChildArea;
				index++;
				getFinalLevelArea(index, areaList, callback);

			}, function(message) {
				//失败
			});
		}
	}
	/*获取城市列表*/
	var getCityList = function() {
		return common.myStorage.getItem('cityList');
	}
	/*获取当前城市的子地区列表*/
	var getCurrentCityChileAreaList = function() {
//		console.log()
		return common.myStorage.getItem('CurrentChildAreaListKey');
	}
	return {
		setNetworkClient: setNetworkClient,
		getAreaListByLevel: getAreaListByLevel,
		getCurrentCity: getCurrentCity,
		setCurrentCityByCityName: setCurrentCityByCityName,
		setCurrentCityByCityObj: setCurrentCityByCityObj,
		getCurrentCityChileAreaList: getCurrentCityChileAreaList,
		getCityList: getCityList,
		getChildAreaListWithParentId:getChildAreaListWithParentAreaId
	}
})();